﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using TruyenCuoi.Entity;
using VideoCookBook.Entity;

namespace TruyenCuoi.ViewModel
{
    public class ListStoryViewModel : BindableBase
    {
        private ListStoryReponse data = new ListStoryReponse();
        private List<ReturnStory> listNews = new List<ReturnStory>();
        private string pageTitle;

        public string PageTitle
        {
            get { return pageTitle; }
            set { SetProperty(ref pageTitle, value); }
        }
        public List<ReturnStory> ListNews
        {
            get { return listNews; }
            set { SetProperty(ref listNews, value); }
        }
        public static string IDStory;
        public static string UrlImage;
        public static string TitlePage;
        public ListStoryViewModel()
        {
            CallListStorty(IDStory);
            PageTitle = TitlePage;
        }

        private void CallListStorty(string IDStory)
        {
            WebClient web = new WebClient();
            web.DownloadStringCompleted += web_DownloadStringCompleted;
            web.DownloadStringAsync(new Uri("http://www.wservice.somee.com/truyencuoi/GetListTruenCuoiWithType/" + IDStory));
        }

        void web_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                data = JsonConvert.DeserializeObject<ListStoryReponse>(e.Result);
                if (data.returnData != null && data.returnData.Count > 0)
                {
                    foreach(var item in data.returnData)
                    {
                        item.UrlImage = UrlImage;
                    }
                    ListNews = data.returnData;
                }
                else MessageBox.Show("No da ta");
            }
        }
    }
}
